<?php
class Index extends Action {
	private $limit = 10;
	private $status = array ('new', 'pass', 'spam', 'deleted' );
	/* (non-PHPdoc)
	 * @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		$status = rqst ( 'status', 'new' );
		$status = in_array ( $status, $this->status ) ? $status : 'new';
		$data ['status'] = $status;
		if ($status == 'deleted') {
			$where ['deleted'] = 1;
		} else {
			$where ['deleted'] = 0;
			$where ['status'] = $status;
		}
		$cname = rqst ( 'cname' );
		if (! empty ( $cname )) {
			$where ['asker_name LIKE'] = "%{$cname}%";
			$data ['cname'] = $cname;
		}
		$uname = rqst ( 'uname' );
		if (! empty ( $uname )) {
			$where ['anwer_name LIKE'] = "%{$uname}%";
			$data ['uname'] = $uname;
		}
		$start = rqst ( 'start', 1 ); // 分页
		$askM = new PlugAskModel ();
		
		$askM->where ( $where )->count ( true )->sort ( array ('ask_time', 'd' ) )->limit ( $this->limit, $start - 1 );
		$items = $askM->retrieve ();
		
		if ($items) {
			$data ['items'] = $items;
			$data ['totalCount'] = $items->countTotal;
		} else {
			$data ['items'] = array ();
			$data ['totalCount'] = 0;
		}
		
		$data ['new_total'] = $askM->where ( array ('deleted' => 0, 'status' => 'new' ) )->count ();
		$data ['pass_total'] = $askM->where ( array ('deleted' => 0, 'status' => 'pass' ) )->count ();
		$data ['spam_total'] = $askM->where ( array ('deleted' => 0, 'status' => 'spam' ) )->count ();
		$data ['deleted_total'] = $askM->where ( array ('deleted' => 1 ) )->count ();
		
		$data ['limit'] = $this->limit;
		$data ['curl'] = unkeepargs ( $this->url, 'start' );
		sess_add ( 'ask_list_url', $this->url );
		return new SmartyView ( $data, 'Index' );
	}
	public function get_ask_operations($opts, $item) {
		$purl = the_plugin_url ( 'ask' );
		if ($item ['deleted']) {
			$opts .= '<a href="./?Ctlr=SetAskStatus&del=0&aid=' . $item ['ask_id'] . '" class="reset-ask tgre"><i class="icon-share-alt"></i>还原</a>';
			$opts .= '<a onclick="return confirm(\'你确定要删除该留言?\')" href="./?Ctlr=DelAsk&aid=' . $item ['ask_id'] . '" class="del-ask tred"><i class="icon-remove-sign"></i>永久删除</a>';
		} else {
			if ($item ['status'] == 'new' || $item ['status'] == 'pass') {
				$opts .= '<a href="' . $purl . '/?Ctlr=EditAsk&aid=' . $item ['ask_id'] . '" class="edit-ask"><i class="icon-edit"></i>编辑</a>';
				$opts .= '<a href="' . $purl . '/?Ctlr=ReplyAsk&ask_id=' . $item ['ask_id'] . '" class="reply-ask"><i class="icon-comment"></i>回复</a>';
			}
			if ($item ['status'] != 'spam') {
				$opts .= '<a href="./?Ctlr=SetAskStatus&s=spam&aid=' . $item ['ask_id'] . '" class="spam-ask torg"><i class="icon-volume-off"></i>垃圾留言</a>';
				$opts .= '<a href="./?Ctlr=SetAskStatus&del=1&aid=' . $item ['ask_id'] . '" class="trash-ask tred"><i class="icon-trash"></i>移至回收站</a>';
			}
			if ($item ['status'] == 'spam') {
				$opts .= '<a href="./?Ctlr=SetAskStatus&s=new&aid=' . $item ['ask_id'] . '" class="new-ask tgre"><i class="icon-volume-up"></i>这不是垃圾留言</a>';
				$opts .= '<a onclick="return confirm(\'你确定要删除该留言?\')" href="./?Ctlr=DelAsk&aid=' . $item ['ask_id'] . '" class="del-ask tred"><i class="icon-remove-sign"></i>永久删除</a>';
			}
		}
		return $opts;
	}
	public function get_ask_bench_options($opts, $status) {
		if ($status == 'deleted') {
			$opts .= '<li><a href="./?Ctlr=SetAskStatus&del=0" class="menu-reset-cmt tgre"><i class="icon-share-alt"></i>还原</a></li>';
			$opts .= '<li><a href="./?Ctlr=DelAsk" class="menu-del-cmt tred"><i class="icon-remove-sign"></i>永久删除</a></li>';
		} else if ($status == 'spam') {
			$opts .= '<li><a href="./?Ctlr=SetAskStatus&s=new" class="menu-new-cmt tgre"><i class="icon-volume-up"></i>这不是垃圾留言</a></li>';
			$opts .= '<li><a href="./?Ctlr=DelAsk" class="menu-del-cmt tred"><i class="icon-remove-sign"></i>永久删除</a></li>';
		} else {			
			$opts .= '<li><a href="./?Ctlr=SetAskStatus&s=spam" class="menu-spam-cmt torg"><i class="icon-volume-off"></i>垃圾留言</a></li>';
			$opts .= '<li><a href="./?Ctlr=SetAskStatus&del=1" class="menu-trash-cmt tred"><i class="icon-trash"></i>移至回收站</a></li>';
		}
		return $opts;
	}
	protected function bindhooks() {
		bind ( 'get_ask_operations', array ($this, 'get_ask_operations' ), 1, 2 );
		bind ( 'get_ask_bench_options', array ($this, 'get_ask_bench_options' ), 1, 2 );
	}
}